<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="配置名称" prop="confName">
        <el-input
          v-model="queryParams.confName"
          placeholder="请输入配置名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审批业务">
        <el-select v-model="queryParams.moduleType" placeholder="请选择审批业务" style="width:100%">
            <el-option
              v-for="dict in dict.type.clr_module_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['clr:apprclrconfig:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['clr:apprclrconfig:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['clr:apprclrconfig:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="apprclrconfigList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="步骤" align="center" prop="confSeq" />
      <el-table-column label="配置名称" align="center" prop="confName" />
      <el-table-column label="审批业务" align="center" prop="moduleType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.clr_module_type" :value="scope.row.moduleType" />
        </template>
      </el-table-column>
      <el-table-column label="审批角色" align="center" prop="roleName" />
      <el-table-column label="未启用账号是否通过" align="center" prop="notAcctPass">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.notAcctPass" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['clr:apprclrconfig:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['clr:apprclrconfig:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改辅导员审批配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" :rules="rules" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="配置名称" prop="confName">
          <el-input v-model="form.confName" placeholder="请输入配置名称" style="width:100%" maxlength="21"/>
        </el-form-item>
        <el-form-item label="步骤" prop="confSeq">
          <el-input v-model="form.confSeq" placeholder="请输入步骤" style="width:100%" maxlength="2"/>
        </el-form-item>
        <el-form-item label="审批角色" prop="apprRole">
          <el-select v-model="form.apprRole" placeholder="请选择审批角色" style="width:100%">
            <el-option
              v-for="item in roleOptions"
              :key="item.roleId"
              :label="item.roleName"
              :value="item.roleId"
              :disabled="item.status == 1"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="审批业务" prop="moduleType">
          <el-select v-model="form.moduleType" placeholder="请选择审批业务" style="width:100%" @change="chengs">
            <el-option
              v-for="dict in dict.type.clr_module_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="未启用账号是否通过" prop="notAcctPass">
          <el-select v-model="form.notAcctPass" placeholder="请选择未启用账号是否通过" style="width:100%">
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listApprclrconfig,
  getApprclrconfig,
  delApprclrconfig,
  addApprclrconfig,
  updateApprclrconfig
} from "@/api/clr/apprclrconfig";
import { getUser } from "@/api/system/user";
import searchForm from "@/components/Search/searchForm.vue";
import { getDicts } from "@/api/system/dict/type";
export default {
  components: { searchForm },
  name: "Apprclrconfig",
  dicts: ["sys_yes_no", "clr_exam_type",'clr_module_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 辅导员审批配置表格数据
      apprclrconfigList: [],
      // 角色选项
      roleOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        confName: null,
        moduleType: ''
      },
      // 表单参数
      form: {
        moduleType: undefined
      },
      addModule: '',
      // 表单校验
      rules: {
        confName: [
            { required: true, message: '请输入活动名称', trigger: 'blur' },
            { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
          ],
          confSeq: [
            { required: true, message: '请输入步骤', trigger: 'blur' },
          ],
          apprRole: [
            { required: true, message: '请选择审批角色', trigger: 'change' }
          ],
          moduleType: [
            { required: true, message: '请选择审批任务', trigger: 'change' }
          ],
          notAcctPass: [
            { required: true, message: '请选择未启用账号是否通过', trigger: 'change' }
          ],
      }
    };
  },
  created() {
    getDicts("clr_module_type").then(res => {
           
            this.addModule = res.data[0].dictValue
            this.queryParams.moduleType = res.data[0].dictValue
            this.getList();
        });
   
  },
  methods: {
    
    /** 查询辅导员审批配置列表 */
    getList() {
      this.loading = true;
      listApprclrconfig(this.queryParams).then(response => {
        this.apprclrconfigList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    chengs() {
      this.$forceUpdate()
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        cacId: null,
        confSeq: null,
        confName: null,
        apprRole: null,
        notAcctPass: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.cacId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
     
      getUser().then(response => {
        this.roleOptions = response.roles;
        this.open = true;
        this.title = "添加辅导员审批配置";
        this.form.moduleType = this.queryParams.moduleType
        console.log(this.queryParams.moduleType,'s')
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // this.reset();
      const cacId = row.cacId || this.ids;
      getApprclrconfig(cacId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改辅导员审批配置";
        getUser().then(response => {
          this.roleOptions = response.roles;
        });
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.cacId != null) {
           
            updateApprclrconfig(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addApprclrconfig(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const cacIds = row.cacId || this.ids;
      this.$modal
        .confirm('是否确认删除辅导员审批配置编号为"' + cacIds + '"的数据项？')
        .then(function() {
          return delApprclrconfig(cacIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "clr/apprclrconfig/export",
        {
          ...this.queryParams
        },
        `apprclrconfig_${new Date().getTime()}.xlsx`
      );
    }
  }
};
</script>
